package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.UserBase;
import cn.com.blueInfo.bpm.person.enums.*;
import cn.com.blueInfo.bpm.person.mapper.UserBaseMapper;
import cn.com.blueInfo.bpm.person.repository.UserBaseRepository;
import cn.com.blueInfo.bpm.person.request.UserBaseDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 员工基本信息表 数据服务实现类
 * @author suxch
 * @since 2025-08-21 05:08:55
 */
@Log4j2
@Service
public class UserBaseRepositoryImpl
        extends ServiceImpl<UserBaseMapper, UserBase>
        implements UserBaseRepository, BaseRepository<UserBase, UserBaseDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增员工基本信息
     * @param userBase 员工基本信息参数
     */
    @Override
    public Integer createUserBase(UserBase userBase) {
        return sqlExecuteResult(save(userBase));
    }

    /**
     * 批量新增员工基本信息
     * @param userBaseList 员工基本信息参数列表
     */
    @Override
    public Integer batchCreateUserBase(List<UserBase> userBaseList) {
        return sqlExecuteResult(saveBatch(userBaseList), userBaseList.size());
    }

    /**
     * 删除员工基本信息
     * @param userBaseId 员工基本信息ID
     */
    @Override
    public Integer deleteUserBase(Long userBaseId) {
        UserBase userBase = new UserBase();
        userBase.setId(userBaseId);
        userBase.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(userBase));
    }

    /**
     * 批量删除员工基本信息
     * @param userBaseIdList 员工基本信息ID列表
     */
    @Override
    public Integer batchDeleteUserBase(List<Long> userBaseIdList) {
        List<UserBase> userBaseList = new ArrayList<>();
        for (Long userBaseId : userBaseIdList) {
            UserBase userBase = new UserBase();
            userBase.setId(userBaseId);
            userBase.setDeleted(BaseDeleted.DELETED);
            userBaseList.add(userBase);
        }
        return sqlExecuteResult(removeBatchByIds(userBaseList), userBaseList.size());
    }

    /**
     * 更新部分员工基本信息
     * @param userBase 员工基本信息参数
     */
    @Override
    public Integer updateUserBasePartial(UserBase userBase) {
        validateUpdateId(userBase.getId());
        LambdaUpdateWrapper<UserBase> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserBase::getId, userBase.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部员工基本信息
     * @param userBase 员工基本信息参数
     */
    @Override
    public Integer updateUserBase(UserBase userBase) {
        validateUpdateId(userBase.getId());
        return sqlExecuteResult(updateById(userBase));
    }

    /**
     * 查询一条员工基本信息
     * @param userBaseId 员工基本信息ID
     */
    @Override
    public UserBase getUserBaseById(Long userBaseId) {
        LambdaQueryWrapper<UserBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBase::getId, userBaseId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询员工基本信息
     * @param userBaseQueryDTO 员工基本信息参数
     */
    @Override
    public Page<UserBase> getUserBasePage(UserBaseDTO.QueryDTO userBaseQueryDTO) {
        Page<UserBase> userBasePage = new Page<>(
                userBaseQueryDTO.getPage(), userBaseQueryDTO.getSize());
        LambdaQueryWrapper<UserBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, userBaseQueryDTO);

        BaseDeleted deleted = userBaseQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(userBasePage, queryWrapper);
        } else {
            return page(userBasePage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 员工基本信息 数量
     * @param userBaseQueryDTO 员工基本信息查询参数
     */
    @Override
    public Long getUserBaseCount(UserBaseDTO.QueryDTO userBaseQueryDTO) {
        LambdaQueryWrapper<UserBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, userBaseQueryDTO);

        BaseDeleted deleted = userBaseQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 员工基本信息
     * @param userBaseQueryDTO 员工基本信息查询参数
     */
    @Override
    public List<UserBase> getUserBaseList(UserBaseDTO.QueryDTO userBaseQueryDTO) {
        LambdaQueryWrapper<UserBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, userBaseQueryDTO);

        BaseDeleted deleted = userBaseQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<UserBase> queryWrapper, UserBaseDTO.QueryDTO userBaseQueryDTO) {

        Integer userNum = userBaseQueryDTO.getUserNum();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userNum), UserBase::getUserNum, userNum);

        String userCode = userBaseQueryDTO.getUserCode();
        queryWrapper.eq(StringUtils.isNotBlank(userCode), UserBase::getUserCode, userCode);

        String userName = userBaseQueryDTO.getUserName();
        queryWrapper.eq(StringUtils.isNotBlank(userName), UserBase::getUserName, userName);

        UserBaseSex sex = userBaseQueryDTO.getSex();
        queryWrapper.eq(ObjectUtils.isNotEmpty(sex), UserBase::getSex, sex);

        String email = userBaseQueryDTO.getEmail();
        queryWrapper.eq(StringUtils.isNotBlank(email), UserBase::getEmail, email);

        LocalDate entryDate = userBaseQueryDTO.getEntryDate();
        queryWrapper.eq(ObjectUtils.isNotEmpty(entryDate), UserBase::getEntryDate, entryDate);

        String workPhone = userBaseQueryDTO.getWorkPhone();
        queryWrapper.eq(StringUtils.isNotBlank(workPhone), UserBase::getWorkPhone, workPhone);

        String mobile = userBaseQueryDTO.getMobile();
        queryWrapper.eq(StringUtils.isNotBlank(mobile), UserBase::getMobile, mobile);

        UserBaseIdentityType identityType = userBaseQueryDTO.getIdentityType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(identityType), UserBase::getIdentityType, identityType);

        String identityNo = userBaseQueryDTO.getIdentityNo();
        queryWrapper.eq(StringUtils.isNotBlank(identityNo), UserBase::getIdentityNo, identityNo);

        LocalDate birthday = userBaseQueryDTO.getBirthday();
        queryWrapper.eq(ObjectUtils.isNotEmpty(birthday), UserBase::getBirthday, birthday);

        String qq = userBaseQueryDTO.getQq();
        queryWrapper.eq(StringUtils.isNotBlank(qq), UserBase::getQq, qq);

        String weChat = userBaseQueryDTO.getWeChat();
        queryWrapper.eq(StringUtils.isNotBlank(weChat), UserBase::getWeChat, weChat);

        String weChatId = userBaseQueryDTO.getWeChatId();
        queryWrapper.eq(StringUtils.isNotBlank(weChatId), UserBase::getWeChatId, weChatId);

        UserBaseUserStatus userStatus = userBaseQueryDTO.getUserStatus();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userStatus), UserBase::getUserStatus, userStatus);

        UserBaseEmploymentType employmentType = userBaseQueryDTO.getEmploymentType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(employmentType), UserBase::getEmploymentType, employmentType);

        String emergencyName = userBaseQueryDTO.getEmergencyName();
        queryWrapper.eq(StringUtils.isNotBlank(emergencyName), UserBase::getEmergencyName, emergencyName);

        String emergencyMobile = userBaseQueryDTO.getEmergencyMobile();
        queryWrapper.eq(StringUtils.isNotBlank(emergencyMobile), UserBase::getEmergencyMobile, emergencyMobile);

        UserBaseEducation education = userBaseQueryDTO.getEducation();
        queryWrapper.eq(ObjectUtils.isNotEmpty(education), UserBase::getEducation, education);

        LocalDate contractStartDate = userBaseQueryDTO.getContractStartDate();
        queryWrapper.eq(ObjectUtils.isNotEmpty(contractStartDate), UserBase::getContractStartDate, contractStartDate);

        LocalDate contractEndDate = userBaseQueryDTO.getContractEndDate();
        queryWrapper.eq(ObjectUtils.isNotEmpty(contractEndDate), UserBase::getContractEndDate, contractEndDate);

        addQueryWrapper(queryWrapper, userBaseQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<UserBase, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                UserBase::getOrderNumber,
                UserBase::getTenantId,
                UserBase::getApplicationId,
                UserBase::getStatus,
                UserBase::getDeleted,
                UserBase::getDeleteUserId,
                UserBase::getDeleteTime,
                UserBase::getRemark,
                UserBase::getCreateUserId,
                UserBase::getCreateTime,
                UserBase::getUpdateUserId,
                UserBase::getUpdateTime,
                UserBase::getExtend1,
                UserBase::getExtend2,
                UserBase::getExtend3);
    }

}
